package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeValueMapper baseAttributeValueMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;
	@Override
	public List<JSONObject> getBaseCategoryList() {
		//1、查询所有分类
		List<BaseCategoryView> allCategoryList = baseCategoryViewMapper.selectList(null);
		//1.1创建List<JSONObject>对象保存所有的一级分类
		ArrayList<JSONObject> JSONCategory1= new ArrayList<>();
		if (CollectionUtil.isNotEmpty(allCategoryList)) {
			//1.2创建map集合，保存一级分类
			Map<Long,List<BaseCategoryView>> map1 = allCategoryList
					.stream()
					.collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
			//1.3遍历map
			for (Map.Entry<Long, List<BaseCategoryView>> entry : map1.entrySet()) {
				//1.4保存一级的id和name
				Long category1Id = entry.getKey();
				String category1Name = entry.getValue().get(0).getCategory1Name();
				//1.5封装一级分类对象
				JSONObject category1 = new JSONObject();
				category1.put("categoryId",category1Id);
				category1.put("categoryName",category1Name);
				//2、查询二级分类
				//2.1创建List<JSONObject>对象保存所有的二级分类
				List<JSONObject> JSONCategory2 = new ArrayList<>();
				//2.2获取二级分类集合
				Map<Long,List<BaseCategoryView>> map2 = entry.getValue()
						.stream()
						.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
				//2.3遍历二级分类集合
				for (Map.Entry<Long, List<BaseCategoryView>> entry2 : map2.entrySet()) {
					//2.4获取二级分类的id和name
					Long category2Id = entry2.getKey();
					String category2Name = entry2.getValue().get(0).getCategory2Name();
					//2.5封装二级分类对象
					JSONObject category2 = new JSONObject();
					category2.put("categoryId",category2Id);
					category2.put("categoryName",category2Name);
					//3、查询三级分类
					//3.1创建List<JSONObject>对象保存所有的三级分类
					List<JSONObject> JSONCategory3 = new ArrayList<>();
					//3.2获取三级分类集合
					Map<Long,List<BaseCategoryView>> map3 = entry2.getValue()
							.stream()
							.collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
					//3.3遍历三级分类集合
					for (Map.Entry<Long, List<BaseCategoryView>> entry3 : map3.entrySet()) {
						//3.4获取三级分类的id和name
						Long category3Id = entry3.getKey();
						String category3Name = entry3.getValue().get(0).getCategory3Name();
						//3.5封装三级分类对象
						JSONObject category3 = new JSONObject();
						category3.put("categoryId",category3Id);
						category3.put("categoryName",category3Name);
						//3.6 三级分类数据添加到List集合中
						JSONCategory3.add(category3);
					}
					//2.6二级分类添加三级数据
					category2.put("categoryChild",JSONCategory3);
					//2.7 二级分类数据添加到List集合中
					JSONCategory2.add(category2);
				}
				//1.6一级分类添加二级分类数据
				category1.put("categoryChild",JSONCategory2);
				//1.7一级分类数据添加到List集合中
				JSONCategory1.add(category1);
			}
		}
		return JSONCategory1;
	}

	@Override
	public List<BaseAttribute> getAttributeByC1Id(Long category1Id) {
		//方法一
		//查询BaseAttribute基本属性
		LambdaQueryWrapper<BaseAttribute> baseAttributeLambdaQueryWrapper = new LambdaQueryWrapper<>();
		baseAttributeLambdaQueryWrapper.eq(BaseAttribute::getCategory1Id,category1Id);
		List<BaseAttribute> baseAttributeList = baseAttributeMapper.selectList(baseAttributeLambdaQueryWrapper);
		//遍历BaseAttributeList集合
		baseAttributeList.forEach(baseAttribute -> {
			//通过baseAttribute的id查询BaseAttributeValues列表
			List<BaseAttributeValue> baseAttributeValues = baseAttributeValueMapper.selectList(new LambdaQueryWrapper<BaseAttributeValue>()
					.eq(BaseAttributeValue::getAttributeId, baseAttribute.getId()));
			//添加BaseAttribute的AttributeValueList属性
			baseAttribute.setAttributeValueList(baseAttributeValues);
		});
		return baseAttributeList;
		//方法二
		//return baseAttributeMapper.getAttributeByC1Id(category1Id);
	}

	@Override
	public BaseCategoryView getCategoryView(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	/**
	 * 根据一级分类id获取三级分类信息
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		//使用一级查询二级分类（获取二级分类ID列表）
		LambdaQueryWrapper<BaseCategory2> baseCategory2LambdaQueryWrapper = new LambdaQueryWrapper<>();
		baseCategory2LambdaQueryWrapper.eq(BaseCategory2::getCategory1Id, category1Id);
		baseCategory2LambdaQueryWrapper.select(BaseCategory2::getId);
		List<BaseCategory2> categoryView2List = baseCategory2Mapper.selectList(baseCategory2LambdaQueryWrapper);
		//遍历二级分类信息，查询三级的信息
		if(CollectionUtil.isNotEmpty(categoryView2List)){
			List<Long> categoryView2IdList = categoryView2List.stream().map(baseCategory2 -> baseCategory2.getId()).collect(Collectors.toList());

				LambdaQueryWrapper<BaseCategory3> baseCategory3LambdaQueryWrapper = new LambdaQueryWrapper<>();
				baseCategory3LambdaQueryWrapper.eq(BaseCategory3::getIsTop,1);
				baseCategory3LambdaQueryWrapper.in(BaseCategory3::getCategory2Id, categoryView2IdList);
				baseCategory3LambdaQueryWrapper.orderByAsc(BaseCategory3::getOrderNum);
				baseCategory3LambdaQueryWrapper.last("LIMIT 7 ");
			List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(baseCategory3LambdaQueryWrapper);
			return baseCategory3List;
		}

		return null;
	}


	/**
	 * 根据一级分类id获取多级分类的信息
	 * @param category1Id
	 * @return
	 */
	@Override
	public JSONObject getBaseCategoryListByC1Id(Long category1Id) {
		//1 使用C1Id查询视图中的数据
		LambdaQueryWrapper<BaseCategoryView> baseCategoryViewLambdaQueryWrapper = new LambdaQueryWrapper<>();
		baseCategoryViewLambdaQueryWrapper.eq(BaseCategoryView::getCategory1Id, category1Id);
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(baseCategoryViewLambdaQueryWrapper);
		//2 创建JSONObject对象,保存一级分类信息
		JSONObject baseCategoryC1 = new JSONObject();
		baseCategoryC1.put("categoryId",category1Id);
		baseCategoryC1.put("categoryName",baseCategoryViewList.get(0).getCategory1Name());
		//3 创建JSONObject对象保存二级分类信息

		List<JSONObject> jsonObject2List = new ArrayList<>();
		//3.1 创建map
		Map<Long, List<BaseCategoryView>> listMap2 = baseCategoryViewList.stream()
				.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		//3.1 遍历视图数据，获取二级分类集合
		for (Map.Entry<Long, List<BaseCategoryView>> longListEntry : listMap2.entrySet()) {
			JSONObject baseCategoryC2 = new JSONObject();
			Long category2Id = longListEntry.getKey();
			baseCategoryC2.put("categoryId",category2Id);
			baseCategoryC2.put("categoryName",longListEntry.getValue().get(0).getCategory2Name());
			//4 创建JSONObject对象保存三级分类信息

			List<JSONObject> jsonObject3List = new ArrayList<>();
			for (BaseCategoryView baseCategoryView : longListEntry.getValue()) {
				JSONObject baseCategoryC3 = new JSONObject();
				Long category3Id = baseCategoryView.getCategory3Id();
				baseCategoryC3.put("categoryId",category3Id);
				baseCategoryC3.put("categoryName",baseCategoryView.getCategory3Name());
				jsonObject3List.add(baseCategoryC3);
			}
			baseCategoryC2.put("categoryChild",jsonObject3List);
			jsonObject2List.add(baseCategoryC2);
		}
		baseCategoryC1.put("categoryChild",jsonObject2List);
		return baseCategoryC1;
	}


}








